/**

 @Name：layuiAdmin（iframe版） 锁屏
 
 
 @License：LPPL
    
 */

layui.define(['setter', 'admin'], function (exports) {
  var $ = layui.$,
    setter = layui.setter,
    admin = layui.admin,
    layer = layui.layer,
    lockscreen = {
      lock: function () {
        window.sessionStorage.setItem("lockcms", true);
        layer.open({
          title: false,
          offset: '150px',
          type: 1,
          anim: 4,
          content: ' <div class="admin-header-lock" id="lock-box">' +
            '<div class="admin-header-lock-img"><img src="layuiAdmin/style/res/face.jpg" /></div>' +
              '<div class="admin-header-lock-name" id="lockUserName">' + layui.sessionData(setter.tableName)["UserInfo"].user.cUserName +
            '</div> <div class="input_btn"> ' +
            '<input type="password" class="admin-header-lock-input layui-input" autocomplete="off" placeholder="请输入密码解锁.." name="lockPwd" id="lockPwd" />' +
            '<button class="layui-btn" id="unlock">解锁</button>' +
            '</div>' +
            '</div>',
          closeBtn: 0,
          //shade: 1
        });
        $(".layui-layer-shade").addClass("lockBg")
        $(".admin-header-lock-input").focus();
      },
      unlock: function (that) {
        if($(that).siblings(".admin-header-lock-input").val() == '') {
          layer.msg("请输入解锁密码！", { offset: '180px' });
          $(that).siblings(".admin-header-lock-input").focus();
        } else {
          admin.req({
            type: 'post',
            url: '/api/Login/UnLock',
              data: { UserName: layui.sessionData(setter.tableName)["UserInfo"].user.cUserName, password: $(that).siblings(".admin-header-lock-input").val() },
            success: function (r) {
              if(r.code == 200) {
                window.sessionStorage.setItem("lockcms", false);
                $(that).siblings(".admin-header-lock-input").val('');
                layer.closeAll("page");
              } else {
                layer.msg(r.msg, { offset: '180px' });
                $(that).siblings(".admin-header-lock-input").val('').focus();
              }
            }
          })
        }
      }
    }

  $(document).on("click", "#unlock", function () {
    lockscreen.unlock(this);
  });
  $(document).on("click", ".lockcms", function () {
    lockscreen.lock();
  });

  // 判断是否显示锁屏
  if(window.sessionStorage.getItem("lockcms") == "true") {
    lockscreen.lock();
  }

  $(document).keyup(function (event) {
    if(event.keyCode == 13) {
      $(document).find("#unlock").click();
    }
  });
  $(document).keydown(function (e) {
    if(e.altKey && e.which == 76) {
      lockscreen.lock();
    }
  });
  exports('lockscreen', lockscreen);
});